using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;

namespace MCEMediaManager
{
    /// <summary>
    /// This object contains the standard set of information displayed in a
    /// content page UI (e.g. Gallery).
    /// </summary>
    public class ContentPage : ModelItem
    {
        /// <summary>
        /// The list of items in the page.
        /// This list should only contain objects of type TorrentItem.
        /// </summary>
        public IList Content
        {
            get {
                if (content == null)
                {
                    content = new ArrayListDataSet();
                }
                return content; 
            }
            set
            {
                if (content != value)
                {
                    content = value;
                    FirePropertyChanged("Content");
                }
            }
        }


        private IList content;
    }

    /// <summary>
    /// This object contains the standard set of objects in a search page.
    /// </summary>
    public class SearchContentPage : ContentPage
    {
        private String _SearchText = "";

        public String SearchText
        {
            get
            {
                return _SearchText;
            }
            set
            {
                if (_SearchText != value)
                {
                    _SearchText = value;
                    OnSearchTextChanged(this, EventArgs.Empty);
                    FirePropertyChanged("SearchText");
                }
            }
        }
        /// <summary>
        /// Executed when the user changes the search criteria
        /// </summary>
        public event EventHandler SearchTextChanged;

        /// <summary>
        /// Fire the event for the search text change.
        /// </summary>
        public virtual void OnSearchTextChanged(object sender, EventArgs args)
        {
            if (SearchTextChanged != null)
                SearchTextChanged(this, args);
        }
    }

    /// <summary>
    /// This object contains the standard set of information displayed in a 
    /// filtered content page UI (e.g. SearchResults).
    /// </summary>
    public class FilteredContentPage : ContentPage
    {
        /// <summary>
        /// A choice of available filters on the content.
        /// </summary>
        public virtual Choice Filters
        {
            get 
            {
                if (filters == null)
                    filters = new Choice();
                return filters; 
            }
            set
            {
                if (filters != value)
                {
                    // Unhook events on the old value
                    if (filters != null)
                    {
                        try
                        {
                            filters.ChosenChanged -= new EventHandler(OnActiveFilterChanged);
                        }
                        catch { }
                    }

                    filters = value;

                    // Hook up events to the new value
                    if (filters != null)
                        filters.ChosenChanged += new EventHandler(OnActiveFilterChanged);

                    // Fire the "chagned" event
                    OnActiveFilterChanged(filters, EventArgs.Empty);
                    FirePropertyChanged("Filters");
                }
            }
        }

        /// <summary>
        /// Fired when the Chosen value within Filters has been modified
        /// or if the instance of Filters has been changed.
        /// </summary>
        public event EventHandler ActiveFilterChanged;

        /// <summary>
        /// Fire the event for the active filter changing.
        /// </summary>
        protected virtual void OnActiveFilterChanged(object sender, EventArgs args)
        {
            if (ActiveFilterChanged != null)
                ActiveFilterChanged(this, EventArgs.Empty);
        }


        private Choice filters;
    }

    /// <summary>
    /// This has two sets of pivots to allow more flexibility to dipslaying information.  Categories 
    /// are the first level and Filters are the second.
    /// </summary>
    public class DualFilteredContentPage : FilteredContentPage
    {
        /// <summary>
        /// A choice of available filters on the content.
        /// </summary>
        public virtual CustomChoice Categories
        {
            get
            {
                if (_Categories == null)
                {
                    _Categories = new CustomChoice();
                }
                return _Categories;
            }
            set
            {
                if (_Categories != value)
                {
                    // Unhook events on the old value
                    if (_Categories != null)
                    {
                        try
                        {
                            _Categories.ChosenChanged -= new EventHandler(OnActiveCategoryChanged);
                        }
                        catch { }
                    }

                    _Categories = value;

                    // Hook up events to the new value
                    if (_Categories != null)
                        _Categories.ChosenChanged += new EventHandler(OnActiveCategoryChanged);

                    // Fire the "chagned" event
                    OnActiveCategoryChanged(_Categories, EventArgs.Empty);
                    FirePropertyChanged("Categories");
                }
            }
        }

        /// <summary>
        /// Fired when the Chosen value within Filters has been modified
        /// or if the instance of Filters has been changed.
        /// </summary>
        public event EventHandler ActiveCategoryChanged;

        /// <summary>
        /// Fire the event for the active filter changing.
        /// </summary>
        protected virtual void OnActiveCategoryChanged(object sender, EventArgs args)
        {
            if (ActiveCategoryChanged != null)
                ActiveCategoryChanged(this, EventArgs.Empty);
        }


        private CustomChoice _Categories;

    }
}
